import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import { UserModule } from './user/user.module';
import { IncomeModule } from './income/income.module';
import { ExpenseModule } from './expense/expense.module';
import { BorrowModule } from './borrow/borrow.module';
import { LendModule } from './lend/lend.module';
import { AccountModule } from './account/account.module';
import { AuthGuard } from './auth/auth.guard';
import { APP_GUARD } from '@nestjs/core';
import { RedisService } from './redis/redis.service'; // 导入 RedisService
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { QiniuController } from './utils/qiniu.controller';
import { QiniuService } from './utils/qiniu.service';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';

@Module({
  imports: [
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'src/static'), // 设置静态文件所在的根路径
      serveRoot: '/static', // 可选：指定访问静态文件的基础URL路径，默认为 '/'
    }),
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: '.env',
    }),
    JwtModule.registerAsync({
      useFactory: async (configService: ConfigService) => ({
        secret: configService.get<string>('JWT_SECRET'),
        signOptions: { expiresIn: '60m' }, // 根据需要调整过期时间
      }),
      inject: [ConfigService],
      global: true,
    }),
    TypeOrmModule.forRootAsync({
      useFactory: (configService: ConfigService) => ({
        type: 'mysql',
        host: configService.get<string>('DATABASE_HOST'),
        port: Number(configService.get<number>('DATABASE_PORT')),
        username: configService.get<string>('DATABASE_USERNAME'),
        password: configService.get<string>('DATABASE_PASSWORD'),
        database: configService.get<string>('DATABASE_NAME'),
        entities: [__dirname + '/**/*.entity{.ts,.js}'],
        synchronize: true,
      }),
      inject: [ConfigService],
    }),
    UserModule,
    IncomeModule,
    ExpenseModule,
    BorrowModule,
    LendModule,
    AccountModule,
  ],
  controllers: [AppController, QiniuController],
  providers: [
    AppService,//根路径的服务
    {
      provide: APP_GUARD,
      useClass: AuthGuard, // 使用自定义的 AuthGuard 作为全局守卫
    },
    RedisService,//redis服务
    QiniuService,//七牛云
  ],
})
export class AppModule { }